﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OrderAggregator.DataStorage;
using OrderAggregator.InputFormats;

namespace OrderAggregator.UI
{
    public partial class OrganizationResolveControl : UserControl
    {
        
        public OrganizationResolveControl()
        {
            InitializeComponent();
        }

        public void Init(Storage storage, BaseOrder order)
        {
            organizationTreeList1.Init(storage.Organization);
            organizationTreeList1.SelectedOrganizationChanged += delegate
            {
                UpdateSelectedOrg();
            };

            txtBoxOrg.Text = order.Client;

            UpdateSelectedResolving();
            UpdateSelectedOrg();
        }

        
        public bool UseExistintOrg
        {
            get
            {
                return rdBtnExistOrg.Checked;
            }
        }

        public Storage.OrganizationRow SelectedOrganization
        {
            get
            {
                return (Storage.OrganizationRow)organizationTreeList1.SelectedOrganization; 
            }
        }


        private void rdBtnExistOrg_CheckedChanged(object sender, EventArgs e)
        {
            UpdateSelectedResolving();
        }

        void UpdateSelectedOrg()
        {
            var org = (Storage.OrganizationRow)organizationTreeList1.SelectedOrganization;

            txtBoxSelectedOrg.Text = org == null ? "" : org.Name;
        }

        private void UpdateSelectedResolving()
        {
            if (rdBtnExistOrg.Checked)
            {
                label2.Text = "Выбранная организация:";
            }
            else
            {
                label2.Text = "Расположение организации в справочнике:";
            }
        }
    }
}
